<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="工号|姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="importRemove">
        <i class="layui-icon layui-icon-reduce-circle"></i>
        批量文件删除
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="import">
        <i class="layui-icon layui-icon-add-circle"></i>
        批量文件导入
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="members">
        <i class="layui-icon layui-icon-add-circle"></i>
        添加学员名单
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="rule">
        <i class="layui-icon layui-icon-tabs"></i>
        设置具体规则
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<script type="text/html" id="table-type">
    {{#if (d.type == '1') { }}
    <span style="color: red;">规则推荐不可删除</span>
    {{# }else if(d.type == '2'){ }}
    <span>可删除</span>
    {{# } }}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let courseid = "{$courseid}";
        let type = "{$type}";
        let cols = [
            [{
                type: 'checkbox',
                fixed: 'left',
            },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center',
                },
                {
                    title: '工号',
                    field: 'carolid',
                    align: 'center',
                },
                {
                    title: 'Division',
                    field: 'division',
                    align: 'center',
                },
                {
                    title: '部门',
                    field: 'department_name',
                    align: 'center',
                },
                {
                    title: '职务',
                    field: 'position',
                    align: 'center',
                },
                {
                    title: '入职时间',
                    field: 'in_time',
                    align: 'center',
                },
                {
                    field: 'type',
                    align: 'center',
                    templet:'#table-type',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Course/getRecommendList?courseid='+courseid+'&type='+type,
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }]
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'edit') {
                window.edit(obj);
            }
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'import') {
                window.import();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'importRemove') {
                window.importRemove(obj);
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'rule') {
                window.rule();
            } else if (obj.event === 'export') {
                window.export();
            } else if (obj.event === 'members') {
                window.members();
            }
        });


        window.rule = function() {
            let msg =  type == 1 ? '可见范围':'软推荐';
            parent.layui.admin.addTab('2200'+type+courseid, courseid+msg+"规则列表", '/Course/rule?courseid='+courseid+'&type='+type)
        }

        window.members = function() {
            if (type == 1) {
                layer.open({
                    type: 2,
                    title: '新增员工',
                    shade: 0.1,
                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                    content: '/Course/members?courseid='+courseid+'&type='+type
                });
            } else {
                layer.open({
                    type: 2,
                    title: '新增员工',
                    shade: 0.1,
                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                    content: '/Course/members1?courseid='+courseid+'&type='+type
                });
            }

        }


        window.importRemove = function() {
            layer.open({
                type: 2,
                title: '批量删除',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Course/deleteUser?courseid='+courseid+'&type='+type
            });
        }

        window.import = function() {
            layer.open({
                type: 2,
                title: '导入',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Course/importUser?courseid='+courseid+'&type='+type
            });
        }

        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出员工数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('exportUser')}?keys="+field.keys+'&courseid='+courseid+'&type='+type, "_blank");
            });
        };



        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'id');
            console.log(checkIds)
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除选中的员工吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Course/recommendDeleteMembers",
                    data:{id:checkIds, courseid:courseid, type:type},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
